// pages/checkpoint/index.js
import {
  config
} from "../../config"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAllJoin: false,
    currentIndex: 0,
    checkpointList: [{
        id: 1,
        isJoin: 0,
      },
      {
        id: 2,
        isJoin: 0,
      },
      {
        id: 3,
        isJoin: 0,
      },
    ],
    openId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(app.globalData)
    this.setData({
      openId: app.globalData.openid
    })
    // setTimeout(() => {
    //   this.setData({
    //     checkpointList: [{
    //         id: 1,
    //         isJoin: 1,
    //       },
    //       {
    //         id: 2,
    //         isJoin: 1,
    //       },
    //       {
    //         id: 3,
    //         isJoin: 1,
    //       }
    //     ]
    //   })
    //   this.setNextBtn()
    // }, 1000)
  },
  getUserData() {
    const openId = this.data.openId
    var userInfo = wx.getStorageSync('userInfo')
    const avatar = escape(userInfo.avatarUrl)
    const nickname = escape(userInfo.nickName)
    wx.request({
      url: `${config.api_blink_url}/redbull/user?openid=${openId}&nickname=${nickname}&avatar=${avatar}`,
      method: "post",
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        const data = res.data
        const checkpointList = [{
            id: 1,
            isJoin: data.g1,
          },
          {
            id: 2,
            isJoin: data.g2,
          },
          {
            id: 3,
            isJoin: data.g3,
          }
        ]
        this.setData({
          checkpointList:checkpointList
        })
        this.setNextBtn()
      }
    })
  },
  setNextBtn() {
    const listData = this.data.checkpointList
    const stateList = []
    listData.forEach((item) => {
      stateList.push(item.isJoin)
    })
    if (stateList.indexOf(0) === -1) {
      this.setData({
        isAllJoin: true
      })
    } else {
      this.setData({
        currentIndex: stateList.indexOf(0)
      })
    }
  },
  goScaneCode(event) { //扫码跳转
    const roundIndex = event.currentTarget.dataset.index
    if (roundIndex === this.data.currentIndex) {
      wx.redirectTo({
        url: '../scanningCode/index?round=' + roundIndex
      })
    }
  },
  goLottery() {
    wx.navigateTo({
      url: '../introduction/index'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserData()
    console.log(this.data.checkpointList)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})